package deom1;

public class Test2 {
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
             val = x;
            next = null;
        }
   }

    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            ListNode pl = headA;
            ListNode ps = headB;
            int lenA = 0;
            int lenB = 0;
            while (pl != null) {
                lenA++;
                pl = pl.next;
            }
            while (ps != null) {
                lenB++;
                ps = ps.next;
            }
            pl = headA;
            ps = headB;
            int len = lenA - lenB;
            if (len < 0) {
                pl = headB;
                ps = headA;
                len = -len;
            }
            while (len !=0) {
                pl = pl.next;
                len--;
            }
            while (pl != ps) {
                pl = pl.next;
                ps = ps.next;
            }
            if (pl ==null) {
                return null;
            }
            return pl;
        }
    }
}
